package com.calvin.study.controller;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.calvin.study.base.ResponseModel;
import com.calvin.study.model.base.ResponseEntity;
import com.calvin.study.service.sys.ISysUserService;
import com.calvin.study.utils.ApplicationContextUtil;
import com.calvin.study.utils.VerifyCodeUtil;

@Controller
public class UserLoginController {

	// 登录
	@PostMapping("/login")
	public @ResponseBody ResponseModel login(String username, String password,String verifycode,HttpSession session) {
		System.out.println("login ...");
		if(StringUtils.isBlank(verifycode)) {
			return ResponseModel.error("请填写验证码");
		}
		try {
			String vcode = (String) session.getAttribute("code");
			if (vcode.equalsIgnoreCase(verifycode)){
				UsernamePasswordToken loginToken = new UsernamePasswordToken(username, password);
				Subject subject = SecurityUtils.getSubject();
				subject.login(loginToken); 
				return ResponseModel.success();
			}else {
				  throw new RuntimeException("验证码错误!");
			}
		} catch (UnknownAccountException e) {
			e.printStackTrace();
			return ResponseModel.error("用户名错误");
		} catch (IncorrectCredentialsException e) {
			e.printStackTrace();
			return ResponseModel.error("密码错误");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseModel.error(e.getMessage());
		}
	}

	// 注册
	@PostMapping("/register")
	public @ResponseBody ResponseEntity register(String username, String password) {
		System.out.println("register ...");
		try {
			ISysUserService sysUserService = (ISysUserService) ApplicationContextUtil.getBean("sysUserService");
			return sysUserService.doRegister(username, password);
		} catch (Exception e) {
			return ResponseEntity.error(e.getMessage());
		}
	}

	/**
	 * 退出登录
	 */
	@RequestMapping("logout")
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();// 退出用户
		return "redirect:/login.html";
	}

	//获取图形验证码
	@RequestMapping("getVerifyCode")
	public void getVerifyCode(HttpSession session, HttpServletResponse response) throws IOException {
		// 生成验证码
		String code = VerifyCodeUtil.generateVerifyCode(4);
		// 验证码放入session
		session.setAttribute("code", code);
		// 验证码存入图片
		ServletOutputStream os = response.getOutputStream();
		response.setContentType("image/png");
		//输出验证码
		VerifyCodeUtil.outputImage(220, 60, os, code);
	}

	// 首页
	@GetMapping("")
	public String index() {
		System.out.println("index ...");
		return "/index";
	}

	@GetMapping("biz1")
	public @ResponseBody ResponseEntity biz1() {
		return ResponseEntity.success("业务1");
	}

	@GetMapping("biz2")
	public @ResponseBody ResponseEntity biz2() {
		return ResponseEntity.success("业务2");
	}

}
